package org.huluobo.service.impl;

import org.huluobo.dao.UserDao;
import org.huluobo.model.User;
import org.huluobo.redis.dao.impl.RedisDaoImpl;
import org.huluobo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;
	@Autowired
	private RedisDaoImpl redisDao;

	@Override
	public boolean add(User user) {
		try {
			userDao.insert(user);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			// 就是这一句了，加上之后，如果抛了异常,会回滚的
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return false;
		}

	}

	public User getByPhone(String phone) {
		return userDao.getByPhone(phone);
	}

	@Override
	public boolean upLoginTime(User record) {
		try {
			userDao.upLoginTime(record);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			// 就是这一句了，加上之后，如果抛了异常,会回滚的
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return false;
		}
	}

	@Override
	public boolean redisSave(User record) {
		try {
			redisDao.save("user", record);
			return true;
		} catch (Exception e) {
			return false;
		}

	}

}
